package com.ultrawise.survey.module.surevybase.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ultrawise.survey.common.utils.DateUtils;
import com.ultrawise.survey.common.utils.RequestGetIp;
import com.ultrawise.survey.common.utils.StringUtils;
import com.ultrawise.survey.core.base.BaseContoller;
import com.ultrawise.survey.core.base.ServiceException;
import com.ultrawise.survey.module.surevybase.service.TSurveyDataAnswerService;
import com.ultrawise.survey.module.surevybase.service.TSurveyInfoBaseService;
import com.ultrawise.survey.module.surevybase.service.TSurveyInfoObjtableService;
import com.ultrawise.survey.module.surevybase.service.TSurveyLogPublishService;
import com.ultrawise.survey.module.surevybase.vo.TSurveyDataAnswer;
import com.ultrawise.survey.module.surevybase.vo.TSurveyInfoBase;
import com.ultrawise.survey.module.surevybase.vo.TSurveyInfoObjtable;
import com.ultrawise.survey.module.surevybase.vo.TSurveyLogPublish;

@Controller
@RequestMapping(value = "/front")
public class FrontControl extends BaseContoller {
	@Autowired
	TSurveyInfoBaseService tSurveyInfoBaseService;
	@Autowired
	TSurveyLogPublishService tSurveyLogPublishService;
	@Autowired
	TSurveyDataAnswerService tSurveyDataAnswerService;
	@Autowired
	TSurveyInfoObjtableService tsurveyinfoobjtableservice;
	//发布时间
		@RequestMapping(value = "frontlist.htm")
		public ModelAndView frontlist(TSurveyInfoBase tbase,
			HttpServletRequest request) throws ServiceException {
			List<TSurveyInfoBase> list = tSurveyInfoBaseService.getStarSurveyInfo();
			//获取最后的发布路径
			List<TSurveyLogPublish> spub = new ArrayList<TSurveyLogPublish>();
			for (int i = 0; list != null && i < list.size(); i++) {
				TSurveyInfoBase tb = list.get(i);
				TSurveyLogPublish tpub = new TSurveyLogPublish();
				Map<String, Object> mpub = tSurveyLogPublishService.getLastPub(tb.getId().toString());
				if (mpub != null) {
					tpub.setId((Integer) mpub.get("ID"));
					tpub.setPu_url((String) mpub.get("PU_URL"));
					spub.add(tpub);
				} else
					
					System.out.println(mpub);
				}
			request.setAttribute("spub", spub);
			return new ModelAndView("/baseinfo/frontlist", "list", list);
		}
		
		
		//commit提交页面
		@RequestMapping(value = "commit.htm")
		public ModelAndView commit(HttpServletRequest request) throws ServiceException {
			return new ModelAndView("/user/commit");
		}
		
		
		
		@RequestMapping(value = "answer.htm")
		public ModelAndView setAnswer(HttpServletRequest request) {
			String syid = request.getParameter("syid");

			// 添加用户对象信息表
			Integer usid = addobjtable(request, syid);

			Enumeration names = request.getParameterNames();
			
			Map<Integer, TSurveyDataAnswer> addanswer = new HashMap<Integer, TSurveyDataAnswer>();
			while (names.hasMoreElements()) {
				TSurveyDataAnswer an = new TSurveyDataAnswer();
				// 设置问卷id信息
				an.setSy_id(Integer.parseInt(syid));
				String name = (String) names.nextElement();
				
				// 当不属于问卷的问题内容的时候信息跳出枚举的循环
				if (name.startsWith("dx") || name.startsWith("fx")
						|| name.startsWith("oth")) {
					// 通过名字截取问题id信息
					String qtidstr = name.substring(name.indexOf("_") + 1,
							name.length());
					Integer qid = (qtidstr != null && StringUtils.isNum(qtidstr)) ? Integer
							.parseInt(qtidstr) : 0;
					an.setQn_id(qid);// 设置问题id
					String choose = "";
					// 获取复选内容信息
					if (name.startsWith("fx")) {
						choose = StringUtils.Join(request.getParameterValues(name),
								"|");
					} else if (name.startsWith("dx")) {
						choose = request.getParameter(name);
					}
					an.setAs_choose(choose);// 设置问题的选项
					if (name.startsWith("oth")) {
						an.setAs_other(request.getParameter(name));// 设置问题的选项
						request.setAttribute("message", "请输入信息！");
					}
					an.setAs_date(DateUtils.toString(new Date(),
							"yyyy-MM-dd HH:mm:ss.SSS"));
					an.setUs_id(usid);//
					try {
						if (addanswer.get(an.getQn_id()) == null) {
							Integer tc = (Integer) tSurveyDataAnswerService
									.saveObj(an);
							an.setId(tc);
							addanswer.put(an.getQn_id(), an);
						} else {
							TSurveyDataAnswer mapan = addanswer.get(qid);
							if (an.getAs_choose() != null
									&& an.getAs_choose().length() > 0) {
								mapan.setAs_choose(an.getAs_choose());
							} else if (an.getAs_other() != null
									&& an.getAs_other().length() > 0) {
								mapan.setAs_other(an.getAs_other());
							}
							tSurveyDataAnswerService.updateObj(mapan);
						}
					} catch (ServiceException e) {
						e.printStackTrace();
					}
				}
			}

			request.setAttribute("message", "您已经成功提交问卷。");
			request.setAttribute("url", request.getContextPath() + "/front/commit.htm");
			return new ModelAndView("/massage");
		}
		// 添加用户基本信息表
		public Integer addobjtable(HttpServletRequest request, String syid) {
			// 使用list进行封装，for循环取值
			List<TSurveyInfoObjtable> adlst = new ArrayList<TSurveyInfoObjtable>();
			try {
				adlst = tsurveyinfoobjtableservice.query("sy_id", syid,
					new TSurveyInfoObjtable(), TSurveyInfoObjtable.class);
			} catch (ServiceException e) {
				e.printStackTrace();
			}
			Map<String, String> map1 = new HashMap<String, String>();
			for (int i = 0; i < adlst.size(); i++) {
				TSurveyInfoObjtable temp = adlst.get(i);
				map1.put(temp.getOb_code(), request.getParameter(temp.getOb_code()));
			}
			map1.put("CREATETIME",
					DateUtils.toString(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
			map1.put("OBJIP", RequestGetIp.getIpAddr(request));
			map1.put("syid", syid);
			// 添加调查用户的对象信息
			return tsurveyinfoobjtableservice.addobjtable(map1);
		}

}
